package com.pitb.citizenreporting;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class TrackIssue extends Activity implements OnClickListener{
	EditText problem ;
	TextView tv ;
	ProgressDialog progressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.track_issue);
		problem = (EditText)findViewById(R.id.track_id);
		tv = (TextView)findViewById(R.id.track_status);
		ImageButton tr = (ImageButton)findViewById(R.id.track);
		tr.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		final String sms = problem.getText().toString();
		if(TextUtils.isEmpty(sms)){
			Toast.makeText(getApplicationContext(), "Enter Track ID", 0).show();
			return;
		}
		showDialog();
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				doTask(sms);
				
			}
		});t.start();
	}
	
	void doTask(String sms){
		String response = HttpPostUtil.trackComplaint(sms);
		if(!TextUtils.isEmpty(response)){
			JsonParser parse = JsonParser.parse(response);
			if(parse != null){
				if(parse.status.equals(JsonParser.ERROR)){
					Message m = handler.obtainMessage(0);
					Bundle b = new Bundle();
					b.putString("msg", parse.message);
					m.setData(b);
					handler.sendMessage(m);
					return;
				}
				if(parse.status.equals(JsonParser.SUCCESS)){					
					Message m = handler.obtainMessage(1);
					Bundle b = new Bundle();
					b.putString("msg", parse.message);
					m.setData(b);
					handler.sendMessage(m);
				}
			}
		}
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what == 0){
				dismiss();
				String s = msg.getData().getString("msg");
				Toast.makeText(getApplicationContext(), s, 0).show();				
			}else{
				dismiss();
				String s = msg.getData().getString("msg");
				tv.setText(s);
			}
		}
	};
	
	static class JsonParser {
		public static final String STATUS = "status";
		public static final String COMPLAINT_ID = "complaint-ID";
		public static final String MESSAGE = "message";
		
		public static final String ERROR = "Error";
		public static final String SUCCESS = "Success";
		
		String status ;
		String message ;
		String complaintId ;
		
		public static JsonParser parse(String response){			
			try {
				JSONObject json = new JSONObject(response);
				JsonParser parse = new JsonParser();
				parse.status = json.getString(STATUS);
				parse.message = json.getString(MESSAGE);
				parse.complaintId = json.getString(COMPLAINT_ID);
				return parse;
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null ;
		}
	}
	
	private void showDialog() {
		progressDialog = ProgressDialog.show(this, "",
				"Checking, Please wait...", true);
	}

	void dismiss() {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}
}
